//
//  CommendScenicSpotViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/3.
//  Copyright © 2016年 KX. All rights reserved.
#import "ModelOfCityServicePoi_list.h"
#import "MapViewController.h"
#import "Masonry.h"
#import "FreeTravelMacro.h"
#import "CollectionViewAlbum.h"
#import "CellOfIMapCommendScenicSpotView.h"
#import "CellOfIconListCommendScenicSpotView.h"
#import "CellOfMostPopularCommendScenicSpotView.h"
#import "FreeTravelNetwork.h"
#import "CityViewController.h"
#import "ModelOfCommendScenicSpotView.h"
#import "ModelOfIconListCommendScenicSpotView.h"
#import "CityCellOfSilkbag.h"
#import "CityCellOfNot_miss.h"
#import "CityCellOfLocalTravelNecessary.h"
#import "ModelOfCityServices.h"
@interface CityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ModelOfCommendScenicSpotView *model;
@property (nonatomic, strong) CollectionViewAlbum *album;
@property (nonatomic, strong) UILabel *labelOfCnname;
@property (nonatomic, strong) UILabel *labelOfEnname;
@property (nonatomic, strong) UILabel *labelOfInfo;
@property (nonatomic, strong) UILabel *labelOfTemp;
@property (nonatomic, strong) ModelOfCityServices *mapModel;
@end

@implementation CityViewController
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setTranslucent:YES];
    [self.navigationController.navigationBar setBarTintColor:[UIColor clearColor]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self handleData];
    [self createTableView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sdfasadfas"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CellOfIconListCommendScenicSpotView class] forCellReuseIdentifier:@"CellOfIconListCommendScenicSpotView"];
    [self.tableView registerClass:[CellOfMostPopularCommendScenicSpotView class] forCellReuseIdentifier:@"CellOfMostPopularCommendScenicSpotView"];
    [self.tableView registerClass:[CellOfIMapCommendScenicSpotView class] forCellReuseIdentifier:@"CellOfMapCommendScenicSpotView"];
    [self.tableView registerClass:[CityCellOfSilkbag class] forCellReuseIdentifier:@"CityCellOfSilkbag"];
    [self.tableView registerClass:[CityCellOfNot_miss class] forCellReuseIdentifier:@"CityCellOfNot_miss"];
    [self.tableView registerClass:[CityCellOfLocalTravelNecessary class] forCellReuseIdentifier:@"CityCellOfLocalTravelNecessary"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    self.album = [[CollectionViewAlbum alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    
    
    
    self.tableView.tableHeaderView = _album;
    self.tableView.tableHeaderView.userInteractionEnabled = NO;
    self.labelOfEnname = [[UILabel alloc]init];
    self.labelOfCnname = [[UILabel alloc]init];
    self.labelOfInfo = [[UILabel alloc]init];
    self.labelOfTemp = [[UILabel alloc]init];
    [self.album addSubview:self.labelOfTemp];
    [self.album addSubview:self.labelOfInfo];
    [self.album addSubview:self.labelOfCnname];
    [self.album addSubview:self.labelOfEnname];
    [self.labelOfCnname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.album.mas_centerX);
        make.width.mas_offset(100);
        make.height.mas_offset(50);
        make.bottom.equalTo(self.album.mas_centerY);
    }];
    [self.labelOfEnname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.album.mas_centerX);
        make.width.mas_offset(100);
        make.height.mas_offset(50);
        make.top.equalTo(self.album.mas_centerY);

    }];
    self.labelOfEnname.textColor = [UIColor whiteColor];
    self.labelOfCnname.textColor = [UIColor whiteColor];
    self.labelOfCnname.textAlignment = NSTextAlignmentCenter;
    self.labelOfEnname.textAlignment = NSTextAlignmentCenter;
    self.labelOfCnname.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.f];
    [self.labelOfInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.album.mas_left).mas_offset(10);
        make.bottom.equalTo(self.album.mas_bottom).mas_offset(-10);
        make.height.mas_offset(50);
        make.width.mas_offset(150);
    }];
    [self.labelOfTemp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(50);
        make.width.mas_offset(100);
        make.left.equalTo(self.labelOfInfo.mas_right).mas_offset(10);
        make.bottom.equalTo(self.album.mas_bottom).mas_offset(-10);
    }];
    self.labelOfInfo.textColor = [UIColor whiteColor];
    self.labelOfInfo.textAlignment = NSTextAlignmentCenter;
    self.labelOfTemp.textColor = [UIColor whiteColor];
    self.labelOfTemp.textAlignment = NSTextAlignmentCenter;

    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger i = 0;
    if (self.model.data.icon_list.count > 0) {
        i++;
        if (self.model.data.city_map.length > 0) {
            i++;
            if (self.model.data.most_popular.count>0) {
                i++;
                if (self.model.data.guide_num > 0) {
                    i++;
                    if (self.model.data.not_miss.count>0) {
                        i++;
                        if (self.model.data.local_basic.count>0) {
                            i++;
                            return i;
                        }
                    }
                }
            }
        }
    }else{
        if (self.model.data.city_map.length > 0) {
            i++;
            if (self.model.data.most_popular.count>0) {
                i++;
                if (self.model.data.guide_num > 0) {
                    i++;
                    if (self.model.data.not_miss.count>0) {
                        i++;
                        if (self.model.data.local_basic.count>0) {
                            i++;
                            return i;
                        }
                    }
                }
            }
        }else{
            if (self.model.data.most_popular.count>0) {
                i++;
                if (self.model.data.guide_num > 0) {
                    i++;
                    if (self.model.data.not_miss.count>0) {
                        i++;
                        if (self.model.data.local_basic.count>0) {
                            i++;
                            return i;
                        }
                    }
                }
            }else{
                if (self.model.data.guide_num > 0) {
                    i++;
                    if (self.model.data.not_miss.count>0) {
                        i++;
                        if (self.model.data.local_basic.count>0) {
                            i++;
                            return i;
                        }
                    }
                }else{
                    if (self.model.data.not_miss.count>0) {
                        i++;
                        if (self.model.data.local_basic.count>0) {
                            i++;
                            return i;
                        }
                    }else{
                        if (self.model.data.local_basic.count>0) {
                            i++;
                            return i;
                        }else{
                            return i;
                        }
                    }
                }
            }
        }
    }
    return i;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 1 && self.model.data.icon_list.count > 0) {
        
        CellOfIconListCommendScenicSpotView *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfIconListCommendScenicSpotView" forIndexPath:indexPath];
        cell.model = self.model.data;
        return cell;
    }if (indexPath.row == 2 && self.model.data.city_map.length > 0){
        CellOfIMapCommendScenicSpotView *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfMapCommendScenicSpotView" forIndexPath:indexPath];
        cell.model = self.model.data;
        return cell;
    }if(indexPath.row == 4 && self.model.data.most_popular.count > 0){
        CellOfMostPopularCommendScenicSpotView *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfMostPopularCommendScenicSpotView" forIndexPath:indexPath];
        cell.model = self.model.data;
        return cell;
    }if(indexPath.row == 0 && self.model.data.guide_num > 0){
        CityCellOfSilkbag *cell = [tableView dequeueReusableCellWithIdentifier:@"CityCellOfSilkbag" forIndexPath:indexPath];
        cell.model = self.model.data;
        return cell;
    }if (indexPath.row == 3 && self.model.data.not_miss.count > 0){
        CityCellOfNot_miss *cell = [tableView dequeueReusableCellWithIdentifier:@"CityCellOfNot_miss" forIndexPath:indexPath];
        cell.model = self.model.data;
        return cell;
    }if(indexPath.row == 5 && self.model.data.local_basic.count>0){
        CityCellOfLocalTravelNecessary *cell = [tableView dequeueReusableCellWithIdentifier:@"CityCellOfLocalTravelNecessary" forIndexPath:indexPath];
        cell.model = self.model.data;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        
        return cell;
        
    }
}
- (void)handleData{
    
        [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/place/city_index?city_id=%ld&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.4&v=1",self.ID] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
            NSLog(@"%ld",self.ID);
            self.model = [ModelOfCommendScenicSpotView modelWithDic:result];
            
            _album.arrOfImages = self.model.data.city_pic;
            self.labelOfCnname.text = self.model.data.cnname;
            self.labelOfEnname.text = self.model.data.enname;
            self.labelOfInfo.text = self.model.data.info;
            if (![self.model.data.high_temp isEqualToString:@""]) {
                self.labelOfTemp.text = [NSString stringWithFormat:@"%@~%@",self.model.data.low_temp,self.model.data.high_temp];
            }
            
            [self.tableView reloadData];
        } andFailureBlock:^(NSError *error) {
            
        }];
       
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 && self.model.data.guide_num > 0) {
        return 50;
    }else if (indexPath.row == 1 && self.model.data.icon_list.count > 0){
        return 150;
    }else if(indexPath.row == 2 && self.model.data.city_map.length > 0){
        return 105;
    }else if(indexPath.row == 3 && self.model.data.not_miss.count > 0){
        return 50+200*self.model.data.not_miss.count;
    }else if(indexPath.row == 4 && self.model.data.most_popular.count > 0){
        return 400;
    }else if(indexPath.row == 5 && self.model.data.local_basic.count>0){
        return 400;
    }else{
        return 0;
    }
}
- (void)goBack:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2 && self.model.data.city_map.length > 0) {
        
        MapViewController *vc = [[MapViewController alloc]init];
        vc.arrOfService = [NSMutableArray array];
        [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/map/poi_list?cate_id=151&city_id=%ld&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=60&order_type=1&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.5&v=1",self.ID] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
            
            self.mapModel = [ModelOfCityServices modelWithDic:result];
            for (ModelOfCityServicePoi_list *model in self.mapModel.data.poi_list) {
                [vc.arrOfService addObject:model];
            }
            
            [self.navigationController pushViewController:vc animated:YES];
        } andFailureBlock:^(NSError *error) {
            
        }];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
